<template>
  <div class="home">
    <transition name="van-fade">
      <div v-if="$route.path == '/home'">
        <!-- 搜索框 -->
        <van-search
          shape="round"
          placeholder="请输入搜索关键词"
          disabled
          @click="searchClick"
        />
        <!-- banner轮播图 -->
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="#1989fa">
          <van-swipe-item v-for="item in banner" :key="item.id">
            <img :src="item.image_url" alt="" width="100%" />
          </van-swipe-item>
        </van-swipe>
        <!-- 网格分类 channel 首页频道 -->
        <Grid :channel="channel" />
        <!-- topic话题 首页专题精选-->
        <Topic :topicList="topicList" />

        <div>
          <p>第1个p标签</p>
          <p>第2个p标签</p>
          <p>第3个p标签</p>
          <p>第4个p标签</p>
          <p>第5个p标签</p>
          <p>第6个p标签</p>
          <p>第7个p标签</p>
          <p>第8个p标签</p>
          <p>第9个p标签</p>
          <p>第10个p标签</p>
        </div>
      </div>
    </transition>
    <!-- 弹框遮罩层 -->
    <transition name="van-fade">
      <div class="popup-shadow" v-show="isShowShadow"></div>
    </transition>

    <transition name="van-slide-right">
      <router-view></router-view>
    </transition>
  </div>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'
import { GetHomeAPI } from "@/request/api.js";
import { mapMutations, mapState } from "vuex";
import Grid from "@/components/home/Grid";
import Topic from "@/components/home/Topic";
export default {
  name: "Home",
  components: {
    Grid,
    Topic,
  },
  data() {
    return {
      banner: [],
      channel: [],
      topicList: [],
    };
  },
  computed: {
    ...mapState({
      isShowShadow: (state) => state.showShadow.isShowShadow,
    }),
  },
  methods: {
    ...mapMutations({
      chanIsShowShadow: "showShadow/chanIsShowShadow",
    }),
    searchClick() {
      this.$router.push("/home/searchpopup");
      this.chanIsShowShadow(true);
    },
  },
  created() {
    GetHomeAPI()
      .then((res) => {
        // console.log(res);
        let { banner, channel, topicList } = res.data;
        this.banner = banner;
        this.channel = channel;
        this.topicList = topicList;
      })
      .catch((err) => {
        console.log(err);
      });
  },
  // 路由的监听而不是路由守卫，只作用于父子级的路由切换有用
  // 有bug最后不采用这个方式用路由守卫去做
  /* watch: {
    $route(to, from) {
      console.log('路由监听 to ', to);
      console.log('路由监听 from ', from);
      if (to.path === '/home') {
        this.chanIsShowShadow(false)
      }
    },
  } */
};
</script>

<style lang = "less" scoped>
/* .slide-enter, .slide-leave-to {
  right: -100%;
}

.slide-enter-active, .slide-leave-active {
  transition: all .5s;
}

.slide-enter-to, .slide-leave {
  right: 0;
} */
.popup-shadow {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
}

.home {
  padding-bottom: 0.5rem;
}
</style>